﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;

public partial class Users_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            //PermissionHelper hh = new PermissionHelper();
            //Session["Permission"] = hh.GetPermission(EUserRole.superadmin);
            btnSave.Visible = false;
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsUserEdit)
                {
                    btnSave.Visible = true;
                    
                }

                if (Request.Params["uid"] != null)
                {

                    CompanyBLL bll = new CompanyBLL();
                    ddlCompany.DataSource = bll.GetAllCompany();
                    ddlCompany.DataTextField = "CompanyName";
                    ddlCompany.DataValueField = "CompanyID";
                    ddlCompany.DataBind();

                    AdminBLL abll = new AdminBLL();
                    ddlUserType.DataSource = abll.GetAllUserType();
                    ddlUserType.DataTextField = "Name";
                    ddlUserType.DataValueField = "ID";
                    ddlUserType.DataBind();

                    ddlUserRole.DataSource = abll.GetAllUserRole();
                    ddlUserRole.DataTextField = "Name";
                    ddlUserRole.DataValueField = "ID";
                    ddlUserRole.DataBind();

                    try
                    {
                        int uid = Convert.ToInt32(Request.Params["uid"]);
                        LoadUser(uid);
                        hUid.Value = Request.Params["uid"];
                    }
                    catch
                    {
                    }
                }

            }

        }

    }

    void LoadUser(int uid)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            UserBLL bll = new UserBLL(permission);
            Staff user = bll.GetUser(uid);
            txtUserName.Text = user.UserName;
            txtCredential.Text = user.Credential;
            txtCurrentIntegral.Text = user.CurrentIntegral.ToString();
            txtIDCard.Text = user.IDCard;
            ddlCompany.SelectedValue = user.CompanyID.ToString();
            ddlUserRole.SelectedValue = user.RoleID.ToString();
            ddlUserType.SelectedValue = user.UserTypeID.ToString();
 

            if (user.Sex == 1)
            {
                rbmale.Checked = true;
            }
            else
            {
                rbfemale.Checked = true;
            }

            cbSafe.Checked = user.IsSafetyEducation == 1 ? true : false;
            cbfine.Checked = user.IsFine  == 1 ? true : false;
            cbAutoDeduct.Checked = user.IsAutoDeductPoints  == 1 ? true : false;
            txtAutoDeductPeriod.Text = user.AutoDeductPeriod.ToString();
            txtAutoDeductPoints.Text = user.AutoDeductPoints.ToString();
        }

    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        //to do:前后端的数据验证
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission != null)
            {
                UserBLL bll = new UserBLL(permission);

                InternalManagementSystem.DAO.Models.Staff user = bll.GetUser(Convert.ToInt32(hUid.Value));

                user.UserName = txtUserName.Text;
                user.CompanyID = Convert.ToInt32(ddlCompany.SelectedValue);
                user.Credential = txtCredential.Text;
                user.CurrentIntegral = Convert.ToInt32(txtCurrentIntegral.Text);
                user.IDCard = txtIDCard.Text;
                user.RoleID = Convert.ToInt32(ddlUserRole.SelectedValue);//用户角色
                user.UserTypeID = Convert.ToInt32(ddlUserType.SelectedValue);//用户种类
 

                if (rbmale.Checked)
                {
                    user.Sex = 1;
                }

                if (rbfemale.Checked)
                {
                    user.Sex = 0;
                }

                user.IsSafetyEducation = cbSafe.Checked ? 1 : 0;
                user.IsFine = cbfine.Checked ? 1 : 0;

                user.IsAutoDeductPoints = cbAutoDeduct.Checked ? 1 : 0;
                user.AutoDeductPeriod = string.IsNullOrEmpty(txtAutoDeductPeriod.Text) ? 0 : Convert.ToInt32(txtAutoDeductPeriod.Text);
                user.AutoDeductPoints = string.IsNullOrEmpty(txtAutoDeductPoints.Text) ? 0 : Convert.ToInt32(txtAutoDeductPoints.Text);

                bll.UpdateUser(user);

                if (Request.Params["cid"] != null)
                {
                    Response.Write("<script> alert( '提交成功！ ');location= 'Index.aspx?cid=" + Request.Params["cid"] + "'</script> ");
                }
                else
                {
                    Response.Write("<script> alert( '提交成功！ ');location= 'Index.aspx'</script> ");
                }
            }
        }
    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        if (Request.Params["cid"] != null)
        {
            Response.Redirect("Index.aspx?cid=" + Request.Params["cid"]);
        }
        else
        {
            Response.Redirect("Index.aspx");
        }
    }
}
